package cn.edu.xmu.orderpayment.payment.Controller;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.orderpayment.payment.Service.PaymentPatternService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;


/**
 * @author Guanlin Wang 22920192204282
 * @date 2021/12/15
 */
@Api(value = "支付渠道", tags = "payment")
@RestController // Restful的Controller对象
@RefreshScope
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
public class PaymentPatternController {

    @Autowired
    private PaymentPatternService paymentPatternService;;

    /**
     * 获得支付渠道的所有状态
     * @return
     */
    @ApiOperation(value = "获得支付渠道的所有状态")
    @ApiResponses({
            @ApiResponse(code = 0, message = "成功"),
    })
    @GetMapping("/paypatterns/states")
    public Object getPaymentPatternStates(){
        ReturnObject ret = paymentPatternService.getPaymentPatternStates();
        return Common.decorateReturnObject(ret);
    }

    /**
     * 获得当前有效的支付渠道
     * return SimplePayPattern
     */
    @ApiOperation(value = "获得当前有效的支付渠道")
    @ApiResponses({
            @ApiResponse(code = 0, message = "成功"),
    })
    @GetMapping("/paypatterns")
    public Object getValidPayPatterns(){
        ReturnObject ret = paymentPatternService.getValidPayPatterns();
        return Common.decorateReturnObject(ret);
    }

    /**
     * 获得所有支付渠道
     * @param shopId
     * @return
     */
    @ApiOperation(value = "获得所有支付渠道")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "authorization ", value = "用户token", required = true, dataType = "String", paramType = "header"),
        @ApiImplicitParam(name = "shopId", value = "管理员id", required = true, dataType = "Long", paramType = "path"),
    })
    @ApiResponses({
        @ApiResponse(code = 0, message = "成功"),
        @ApiResponse(code = 505, message = "操作的资源id不是自己的对象"),
    })
    @GetMapping("/shops/{shopId}/paypatterns")
    @ResponseBody // 为了解决测试过程中 Content type not set 引入 @responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后，写入到response对象的body区，通常用来返回JSON数据或者是XML数据。注意：在使用此注解之后不会再走视图处理器，而是直接将数据写入到输入流中，他的效果等同于通过response对象输出指定格式的数据。
    public Object getAllPayPatterns(
            @RequestHeader("authorization") String authorization,
            @PathVariable(value = "shopId", required = true) Long shopId// 管理员id
    ){
        if(shopId != 0L)  return Common.decorateReturnObject(new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE, "操作的资源id不是自己的对象"));//shopId只能为0 否则返回505错误
        // 一旦验证过当前操作者为管理员直接返回支付渠道即可
        ReturnObject ret = paymentPatternService.getAllPayPatterns();
        return Common.decorateReturnObject(ret);
    }
}
